package io.zim.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zim.modules.sys.dao.SysRoleDeptDao;
import io.zim.modules.sys.entity.SysRoleDeptEntity;
import io.zim.modules.sys.service.SysRoleDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色与部门对应关系
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017年6月21日 23:42:30
 */
@Service("sysRoleDeptService")
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptDao, SysRoleDeptEntity> implements SysRoleDeptService {

  @Override
  @Transactional
  public void saveOrUpdate(Long roleId, List<Long> deptIdList) {
    //先删除角色与菜单关系
    baseMapper.deleteByRoleId(roleId);

    if (deptIdList.size() == 0) {
      return ;
    }

    //保存角色与菜单关系
    Map<String, Object> map = new HashMap<>();
    map.put("roleId", roleId);
    map.put("deptIdList", deptIdList);
    baseMapper.save(map);
  }

  @Override
  public List<Long> queryDeptIdList(Long roleId) {
    return baseMapper.queryDeptIdList(roleId);
  }

  @Override
  public List<Long> queryDeptIdListByUserId(Long userId) {
    return baseMapper.queryDeptIdListByUserId(userId);
  }

}